<template>
  <div class="main-container">
    <el-form :model="query" inline label-width="80px">
      <el-form-item>
        <el-button v-permission="'sys:role:save'" type="primary" @click="openFormModal(null)">添加角色</el-button>
      </el-form-item>
      <el-form-item>
        <el-input v-model="query.roleName" placeholder="请输入角色名称" prefix-icon="Search" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery()"> 查询</el-button>
        <el-button @click="resetQuery()">重置</el-button>
        <common-sort @sort="handleSort" />
      </el-form-item>
    </el-form>
    <el-table v-loading="page.loading" :data="page.list" row-key="id" class="drag-table">
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="状态">
        <template #default="scope">
          <enum-view :enum-name="'status'" :enum-value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" :width="250">
        <template #default="scope">
          <el-button v-permission="'sys:role:edit'" link type="primary" @click="openFormModal(scope.row)">编辑 </el-button>
          <el-divider direction="vertical" />
          <el-button v-permission="'sys:role:menu:set'" link type="primary" @click="handSetMenu(scope.row)">分配菜单 </el-button>
          <el-divider direction="vertical" />
          <el-dropdown>
            <el-button link type="primary"
              >更多操作
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <div v-permission="'sys:role:edit'" @click="handleStatus(scope.row)">
                  <el-dropdown-item v-if="scope.row.status === 1">禁用 </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.status === 0">启用 </el-dropdown-item>
                </div>
              </el-dropdown-menu>
              <div v-permission="'sys:role:delete'" @click="handleDelete(scope.row, '确认删除当前角色【' + scope.row.roleName + '】？')">
                <el-dropdown-item>删除 </el-dropdown-item>
              </div>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <common-pagination v-model:page="page" @pagination="handlePage" />
  </div>
  <role-form ref="formRef" @refresh="handlePage" />
  <menu-set v-if="menuVisible" ref="menuRef" @refresh="handleMenu"></menu-set>
</template>
<script setup>
  import { systemApi } from '@/api/system'
  import { nextTick, ref } from 'vue'
  import useTable from '@/utils/table'
  import RoleForm from './RoleForm.vue'
  import MenuSet from './MenuSet.vue'
  import CommonPagination from '@/components/Common/Pagination/index.vue'
  import EnumView from '@/components/Enum/View/index.vue'
  import CommonSort from '@/components/Common/Sort/index.vue'

  // 设置菜单
  const menuRef = ref()
  const menuVisible = ref(false)
  const handSetMenu = (item) => {
    menuVisible.value = true
    nextTick(() => {
      menuRef.value.onOpen(item)
    })
  }
  const handleMenu = () => {
    menuVisible.value = false
  }
  // 添加/修改
  const formRef = ref()
  const openFormModal = (item) => {
    formRef.value.onOpen(item)
  }

  // 基础功能
  const { page, handlePage, query, handleQuery, resetQuery, handleDelete, handleStatus, handleSort } = useTable({
    page: systemApi.sysRolePage,
    delete: systemApi.sysRoleDelete,
    status: systemApi.sysRoleUpdateStatus,
    sort: systemApi.sysRoleSort
  })
</script>
